(function() {
    angular.module('app').component('tableShopProds', {
        templateUrl: 'app/components/table.list.component/table.shop.prods/table.shop.prods.html',
        controller: tableShopComponent,
        controllerAs: 'vm',
        bindings: {
            // shopId: '=',
            // brandName: '=',
            componentId: '<'
        }
    });


    //productBasicService来自services->product.basic.service
    tableShopComponent.$inject = ['productBasicService','$rootScope', '$scope', 'tableShopComponentService', '$loading'];

    function tableShopComponent(productBasicService,$rootScope, $scope, tableShopComponentService, $loading) {
        var vm = this;

      
        //vm.shopId
        vm.filter = {
            begin: 0,
            // uid: '',
            // brand:'',
            count: 10,
            sort: 'down',
            newPageNumber: 1,
            sortColumn: 'sell_money'
        };

        //table data
        vm.table = {
            count: 0,
            products: []
        };

        //排序
        vm.sort = sort; //function

        //分页
        vm.pageChanged = pageChanged; //function

        //获取日监控
        vm.getMonitorData = productBasicService.getMonitorData; //function

        


        //店铺商品数据
        var clearShop = $rootScope.$on('formShop_detail_EV', function(e, shopId) {
         
            vm.filter.uid = shopId;
            if (vm.filter.brand) {
                delete vm.filter.brand;
            }
            if(vm.componentId == "shopProduct"){ //防止组件被重复调用
                init();
            }
            
        });
        //品牌店铺商品数据
        var clearBrandShop = $rootScope.$on('formBrandShop_detail_EV', function(e, shopId) {
         
            vm.filter.uid = shopId;
            if (vm.filter.brand) {
                delete vm.filter.brand;
            }
            if(vm.componentId == "brandShopProduct"){ //防止组件被重复调用
                init();
            }
            
        });

        //品牌商品数据
        var clearBrand = $rootScope.$on('formBrand_detail_EV', function(e, brandName) {
        
            vm.filter.brand = brandName;

            // console.log('formBrand_detail_EV $ononon');
            if (vm.filter.uid) {
                delete vm.filter.uid;
            }
            if(vm.componentId == "brandProduct"){//防止组件被重复调用
                init();
            }
            
        });

        function pageChanged(newPageNumber) {
            vm.filter.newPageNumber = newPageNumber;
            vm.filter.begin = (vm.filter.newPageNumber - 1) * vm.filter.count;
            init();
        }

        function init() {
            var json = angular.copy(vm.filter);
            //reset table
            $loading.start('tableShopLoading');

            vm.table.products = [];

            //组件id 品牌商品数据
            if (vm.componentId == "brandProduct") {

                tableShopComponentService.getBrandProdsDetail(json).then(function(resp){
                    // console.log(resp);
                    vm.table.count = 0;
                    
                    vm.table.count = resp.count / 10 * 11;
                    vm.table.products = resp.resultProducts;

                    productBasicService.monitorDataInit();

                    $loading.finish('tableShopLoading');
                },function(err){
                    vm.table.count = 0;
                    vm.table.products.length = 0;
                    $loading.finish('tableShopLoading');
                });
            } else {
                // 店铺商品数据
                tableShopComponentService.getShopDetail(json).then(function(resp) {
                    vm.table.count = 0;
                    
                    vm.table.count = resp.count / 10 * 11;

                    productBasicService.monitorDataInit();

                    vm.table.products = resp.products;
                    $loading.finish('tableShopLoading');
                }, function(err) {
                    vm.table.count = 0;
                    vm.table.products.length = 0;
                    $loading.finish('tableShopLoading');
                });

            }

        }



        function sort(sortColumn) {

            if(vm.filter.sortColumn == sortColumn){
                return;
            }
            vm.filter.sortColumn = sortColumn;
            init();
        }



        $scope.$on('$destroy', function() {
            clearShop();
            clearBrandShop();
            clearBrand();
        });

    }
})();
